[Java] Cannot draw pixels

Posted by Wilhelm on Stack Overflow See other posts from Stack Overflow or by Wilhelm
Published on 2010-04-13T03:11:00Z Indexed on 2010/04/13 3:12 UTC
Read the original article Hit count: 503

Filed under:
|
|

Hello everyone.

I want to print each digit of pi number as a colored pixel, so, I get na input, with the pi number, then parse it into a list, each node containing a digit (I know, I'll use an array later), but I never get this painted to screen... Can someone help me to see where I'm wrong?

package edu.pi.view;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.MemoryImageSource;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JPanel
{
 private static final long serialVersionUID = 6416932054834995251L;

 private static int pixels[];
 private static List<Integer> pi;

 public static void readFile(String name)
 {
  File file = new File(name);
  BufferedReader reader = null;
  pi = new ArrayList<Integer>();
  char[] digits;

  try
  {
   reader = new BufferedReader(new FileReader(file));

   String text = null;

   while((text = reader.readLine()) != null)
   {
    digits = text.toCharArray();

    for(char el : digits)
     if(el != ' ')
     pi.add(Character.getNumericValue(el));
   }
  } catch (Exception e)
  {
   e.printStackTrace();
  }
 }

 public void paint(Graphics gg) 
 {
  readFile("c:\\pi.txt");
  int h = 5;
     int w = 2;
     int color = 0xffffff;
     int digit;
     int i = 0; 

     pixels = new int[w * h];

     for (int y = 0; y < h; y++) 
  {
       for (int x = 0; x < h; x++) 
    {
        digit = pi.get(i);

        if(digit == 0)
         color = 0x000000;
        else if(digit == 1)
         color = 0x787878;
        else if(digit == 2)
         color = 0x008B00;
        else if(digit == 3)
         color = 0x00008B;
        else if(digit == 4)
         color = 0x008B8B;
        else if(digit == 5)
         color = 0x008B00;
        else if(digit == 6)
         color = 0xCDCD00;
        else if(digit == 7)
         color = 0xFF4500;
        else if(digit == 8)
         color = 0x8B0000;
        else if(digit == 9)
         color = 0xFF0000;

        pixels[i] = color;
        i++;
       }
     }

     Image art = createImage(new MemoryImageSource(w, h, pixels, 0, w));

     gg.drawImage(art, 0, 0, this);
 }

 public static void main(String[] args)
 {
     JFrame frame = new JFrame();
     frame.getContentPane().add(new Main());

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setSize(300,300);
     frame.setVisible(true);
 }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about pi